/*************************************************************************
	> File Name:    main.cpp
	> Author:       陈猛
	> Mail:         cm_lyws@163.com
	> Created Time: 2024年06月27日 星期四 22时50分51秒
	> Description:  
 ************************************************************************/

#include <events/common_log.h>
#include "ssicloud_trip_dashboard.h"
#include "ssicloud_energy_analysis.h"
#include "ssicloud_behaviour_summary.h"
#include "ssicloud_apptrip_report.h"
#include "ssicloud_vs_maintain.h"
#include "ssicloud_vs_eventtrack.h"
#include "ssicloud_vs_feedback.h"
#include "ssicloud_treasure.h"
#include "ssicloud_classic_case.h"
#include "ssicloud_oil_analysis_refuel.h"
#include "ssicloud_survey_select.h"
#include "ssicloud_service_policy.h"
#include "ssicloud_news.h"
#include "common_func/image_cache_manage.h"

using namespace ssicloud;


class GlobalState : public UINotifyIface
{
public:
    void dataPrepare(int objname, void *obj) {
        GEN_Printf(LOG_DEBUG, "load over.");

        switch (objname) {
        case OBJECT_NAME_TripDashboard:
        {
            TripDashboard *req = reinterpret_cast<TripDashboard *>(obj);
            TripNode &mItem = req->item();
            GEN_Printf(LOG_INFO, "TripDashboard[%p] request ok", req);

            GEN_Printf(LOG_INFO, "---------数据总览-------");
            GEN_Printf(LOG_INFO, "能耗: %s", mItem.fuels.c_str());
            GEN_Printf(LOG_INFO, "油耗: %s", mItem.oilFuel.c_str());
            GEN_Printf(LOG_INFO, "气耗: %s", mItem.gasFuel.c_str());
            GEN_Printf(LOG_INFO, "里程: %s", mItem.miles.c_str());
            GEN_Printf(LOG_INFO, "运行时长: %s", mItem.times.c_str());
            GEN_Printf(LOG_INFO, "平均速度: %s", mItem.svgSpeed.c_str());
            GEN_Printf(LOG_INFO, "平均能耗: %s", mItem.kmFuels.c_str());
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_EnergyAnalysis:
        {
            EnergyAnalysis *req = reinterpret_cast<EnergyAnalysis *>(obj);
            ListTable<EmergyLNode> &mNodes = req->nodes();
            ListTable<EmergyLNode>::iterator it = mNodes.begin();
            GEN_Printf(LOG_INFO, "EnergyAnalysis[%p] request ok", req);

            GEN_Printf(LOG_INFO, "---------能耗分析---------");
            for (; it != mNodes.end(); ++it)
            {
                GEN_Printf(LOG_DUMP, "日期:%s, 能耗:%s, 里程:%s,百公里能耗:%s", it->day.string(), it->fuels.c_str(), it->miles.c_str(), it->kmFuel.string());
            }
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_BehaviorScore:// 驾驶行为-分数
        {
            BehaviorScore *req = reinterpret_cast<BehaviorScore *>(obj);
            BehaviorScoreNode &mItem = req->item();
            GEN_Printf(LOG_INFO, "BehaviorScore[%p] request ok", req);

            GEN_Printf(LOG_INFO, "---------驾驶行为-分数--------");
            GEN_Printf(LOG_DUMP, "分数数: %g, %s, %s", mItem.score, mItem.chassisNo.c_str(), mItem.plateNo.c_str());
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_BehaviourSummary: // 驾驶行为-总览
        {
            BehaviourSummary *req = reinterpret_cast<BehaviourSummary *>(obj);
            ListTable<BehaviourLNode> &mNodes = req->nodes();
            ListTable<BehaviourLNode>::iterator it = mNodes.begin();

            GEN_Printf(LOG_INFO, "BehaviourSummary[%p] request ok", req);
            GEN_Printf(LOG_INFO, "---------驾驶行为---------");
            for (; it != mNodes.end(); ++it)
            {
                GEN_Printf(LOG_DUMP, "type: %s, %s, 次数: %s", it->type.string(), it->name.c_str(), it->cnt.c_str());
            }
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_BehaviourClassifySummary:
        {
            BehaviourClassifySummary *req = reinterpret_cast<BehaviourClassifySummary *>(obj);
            BehaviourClassifySummaryNode &mItem = req->item();

            GEN_Printf(LOG_INFO, "---------驾驶行为-分类统计查询--------");
            GEN_Printf(LOG_DUMP, "总能耗: %s", mItem.fuels.c_str());
            GEN_Printf(LOG_DUMP, "总时长: %s", mItem.time.c_str());
            GEN_Printf(LOG_DUMP, "总次数: %s", mItem.num.c_str());
            GEN_Printf(LOG_DUMP, "总里程: %s", mItem.miles.c_str());
            GEN_Printf(LOG_DUMP, "总油耗: %s", mItem.oilFuel.c_str());
            GEN_Printf(LOG_DUMP, "总电耗: %s", mItem.electricFuel.c_str());
            GEN_Printf(LOG_DUMP, "总气耗: %s", mItem.gasFuel.c_str());
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_ApptripReport:
        {
            ApptripReport *req = reinterpret_cast<ApptripReport *>(obj);
            ApptripNode &mItem = req->item();

            GEN_Printf(LOG_INFO, "ApptripReport[%p] request ok", req);
            GEN_Printf(LOG_INFO, "---------行程分析-行程统计-------");
            GEN_Printf(LOG_INFO, "avgTrips: %s", mItem.avgTrips.c_str());
            GEN_Printf(LOG_INFO, "trips: %s", mItem.trips.c_str());
            GEN_Printf(LOG_INFO, "days: %s", mItem.days.c_str());
            GEN_Printf(LOG_INFO, "hours: %s", mItem.hours.c_str());
            GEN_Printf(LOG_INFO, "kmFuels: %s", mItem.kmFuels.c_str());
            GEN_Printf(LOG_INFO, "fuels: %s", mItem.fuels.c_str());
            GEN_Printf(LOG_INFO, "miles: %s", mItem.miles.c_str());
            GEN_Printf(LOG_INFO, "avgSpeed: %s", mItem.avgSpeed.c_str());
            GEN_Printf(LOG_INFO, "times: %s", mItem.times.c_str());
            GEN_Printf(LOG_INFO, "avgFuels: %s", mItem.avgFuels.c_str());
            GEN_Printf(LOG_INFO, "svgSpeed: %s", mItem.svgSpeed.c_str());
            GEN_Printf(LOG_INFO, "oilFuel: %s", mItem.oilFuel.c_str());
            GEN_Printf(LOG_INFO, "electricFuel: %s", mItem.electricFuel.c_str());
            GEN_Printf(LOG_INFO, "gasFuel: %s", mItem.gasFuel.c_str());
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_LocationTravel:
        {
            LocationTravel *req = reinterpret_cast<LocationTravel *>(obj);
            LocationTravelNode &mItem = req->item();

            GEN_Printf(LOG_INFO, "LocationTravel[%p] request ok", req);
            GEN_Printf(LOG_INFO, "---------行程分析-按时间查询-------");

             GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_ApptripDistribution:
        {
            ApptripDistribution *req = reinterpret_cast<ApptripDistribution *>(obj);
            ListTable<ApptripDistributionNode> &mNodes = req->nodes();
            ListTable<ApptripDistributionNode>::iterator it = mNodes.begin();

            GEN_Printf(LOG_INFO, "ApptripDistribution[%p] request ok", req);
            GEN_Printf(LOG_INFO, "---------行程分析-油门踏板分布-------");
            for (; it != mNodes.end(); ++it)
            {
                // GEN_Printf(LOG_DUMP, "日期:%s, 能耗:%s, 里程:%s,百公里能耗:%s", it->day.string(), it->fuels.c_str(), it->miles.c_str(), it->kmFuel.string());
            }
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_ApptripDistributionRpm:
        {
            GEN_Printf(LOG_INFO, "---------行程分析-转速分布-------");
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_ApptripDistributionSpeed:
        {
            GEN_Printf(LOG_INFO, "---------行程分析-速度分布-------");
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_TreasureType:   // 用车宝典-获取宝典类型
        {
            GEN_Printf(LOG_INFO, "---------用车宝典-获取宝典类型-------");
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_TreasureCar:    // 用车宝典-分页查询
        {
            GEN_Printf(LOG_INFO, "---------用车宝典-分页查询-------");
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_ClassicTypePage:    // 案例分析-获取案例类型
        {
            GEN_Printf(LOG_INFO, "---------案例分析-获取案例类型-------");
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_ClassicCasePage:    // 案例分析-分页查询
        {
            GEN_Printf(LOG_INFO, "---------案例分析-分页查询-------");
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_OilAnalysisRefuel: // 加油记录
        {
            GEN_Printf(LOG_INFO, "---------加油记录-------");
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_VSMaintain:
        {
            VSMaintain *req = reinterpret_cast<VSMaintain *>(obj);
            MaintainNode &mItem = req->item();

            GEN_Printf(LOG_INFO, "VSMaintain[%p] request ok", req);
            GEN_Printf(LOG_INFO, "---------维保服务 获取里程信息-------");
            GEN_Printf(LOG_INFO, "vin: %s", mItem.vin.c_str());
            GEN_Printf(LOG_INFO, "设置里程: %d", mItem.additionalMile);
            GEN_Printf(LOG_INFO, "距离下次保养里程: %0.02g", mItem.remainMile);
            GEN_Printf(LOG_INFO, "当前里程: %0.02g", mItem.currentMile);
            GEN_Printf(LOG_INFO, "总行使里程: %0.02g", mItem.maintainMile);
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_VSMaintainSet:
        {
            VSMaintainSet *req = reinterpret_cast<VSMaintainSet *>(obj);
            GEN_Printf(LOG_INFO, "VSMaintainSet[%p] request ok", req);
            break;
        }
        case OBJECT_NAME_VSMaintainGetRemind:      // 保养-获取保养提醒
        {
            GEN_Printf(LOG_INFO, "---------保养-获取保养提醒--完成-------");
            break;
        }
        case OBJECT_NAME_VSMaintainRecordAdd:      // 保养-保养登记
        {
            GEN_Printf(LOG_INFO, "---------保养-保养登记--完成-------");
            break;
        }
        case OBJECT_NAME_VSMaintainRecordPage:          // 保养-分页查询保养登记
        {
            GEN_Printf(LOG_INFO, "---------保养-分页查询保养登记--完成-------");
            break;
        }
        case OBJECT_NAME_SurveySelectQuery:  // 问卷调查 - 分页查询
        {
            GEN_Printf(LOG_INFO, "---------问卷调查 - 分页查询--完成-------");
            break;
        }
        case OBJECT_NAME_SurveySelectDetail: // 问卷调查 - 详情
        {
            GEN_Printf(LOG_INFO, "---------问卷调查 - 详情--完成-------");
            break;
        }
        case OBJECT_NAME_ServicePolicy:      // 服务政策-分页查询
        {
            GEN_Printf(LOG_INFO, "---------服务政策-分页查询--完成-------");
            break;
        }
        case OBJECT_NAME_NewsTypeList:       // 新闻资讯-获取资讯类型列表
        {
            GEN_Printf(LOG_INFO, "---------新闻资讯-获取资讯类型列表--完成-------");
            break;
        }
        case OBJECT_NAME_NewsSelectPage:    // 新闻资讯-分页查询
        {
            GEN_Printf(LOG_INFO, "---------新闻资讯-分页查询--完成-------");
            break;
        }

        case OBJECT_NAME_VSEventTrack:
        {
            VSEventTrack *req = reinterpret_cast<VSEventTrack *>(obj);
            GEN_Printf(LOG_INFO, "VSEventTrack[%p] request ok", req);
            break;
        }
        case OBJECT_NAME_VSFeedback:
        {
            VSFeedback *req = reinterpret_cast<VSFeedback *>(obj);
            GEN_Printf(LOG_INFO, "VSFeedback[%p] request ok", req);
            break;
        }
        default:
            GEN_Printf(LOG_INFO, "unkown object");
            break;
        }

        ++ok_times;
    }
    void errorInfo(int objname, void *obj, int type, const ByteString &str)
    {
        GEN_Printf(LOG_INFO, "obj[%d] = %p request failed", objname, obj);
        GEN_Printf(LOG_ERROR, "load error, type=%d, err: %s.", type, str.c_str());
        ++err_times;
    }
    int totals() {return ok_times + err_times;}

    int ok_times = 0;
    int err_times = 0;
};

class GlobalImageState : public ImageStatus
{
public:
    virtual void dataPrepare()
    {
        GEN_Printf(LOG_ERROR, "图片下载完成，可以进行显示了");
    };
    virtual void errorInfo(int , const char *)
    {
        GEN_Printf(LOG_ERROR, "图片下载错误");
    }
    virtual void setLocalFile(ByteString const &path) {
        GEN_Printf(LOG_DEBUG, "图片文件保存的位置", path.c_str());
    }
};

/*
无效错误接口:
    TreasureCar -- 用车宝典 - 分页查询

未实现:
//    14 乘龙之家APP二维码

*/
int main(int /*argc*/, char **/*argv*/)
{
    static GlobalState state;
    static GlobalImageState imageState;
    int total_request = 0;

    setbuf(stderr, NULL);
    setbuf(stdout, NULL);

    if (0){
        // 数据总览
        static TripDashboard req;
        // 设置接口状态通知
        req.setUINotify(&state);
        // 开始请求，并获取数据
        req.requestAll();
        ++total_request;
    }

    if (0){
        // 能耗数据下载
        static EnergyAnalysis req;
        req.setUINotify(&state);
        // 开始请求，并获取数据
        req.requestAll();
        ++total_request;
    }

    if (0) {
        // 驾驶行为-分数
        if (0){
            static BehaviorScore req;
            req.setUINotify(&state);
            // 开始请求，并获取数据
            req.requestAll();
            ++total_request;
        }

        // 驾驶行为-总览
        if (0){
            static BehaviourSummary req;
            req.setUINotify(&state);
            // 开始请求，并获取数据
            req.requestAll();
            ++total_request;
        }
        // 驾驶行为-分类统计查询
        {
            static BehaviourClassifySummary req;
            req.setUINotify(&state);
            // 开始请求，并获取数据
            req.requestAll("4100");
            ++total_request;
        }

    }

    if (0) {
        // 行程分析-行程统计
        if (0){
            // 行程分析-行程统计
            static ApptripReport req;
            req.setUINotify(&state);
            // 开始请求，并获取数据
            req.requestAll();
            ++total_request;
        }
        // 行程分析-按时间查询
        if (0){
            static LocationTravel req;
            req.setUINotify(&state);
            // 开始请求，并获取数据
            req.requestAll();
            ++total_request;
        }
        if (0){
            static ApptripDistribution req;
            req.setUINotify(&state);
            req.requestAll();
            ++total_request;
        }
        {
            static ApptripDistributionRpm req;
            req.setUINotify(&state);
            req.requestAll();
            ++total_request;
        }
        {
            static ApptripDistributionSpeed req;
            req.setUINotify(&state);
            req.requestAll();
            ++total_request;
        }
    }

    if (0) {
        if(1){
            static TreasureType req;
            req.setUINotify(&state);
            req.requestAll();
            ++total_request;
        }

        if (0){
            // error 接口不存在
            static TreasureCar req;
            req.setUINotify(&state);
            req.requestAll("操作指南");
            ++total_request;
        }
    }

    if (0) {
        if(0){
            static ClassicTypePage req;
            req.setUINotify(&state);
            req.requestAll();
            ++total_request;
        }

        if (1){
            static ClassicCasePage req;
            req.setUINotify(&state);
            req.requestAll();
            ++total_request;
        }
    }

    if (0) {
        static OilAnalysisRefuel req;
        req.setUINotify(&state);
        req.requestAll();
        ++total_request;
    }

    if (0) {
        // 维保服务
        if(0){
            static VSMaintain req;
            req.setUINotify(&state);
            // 开始请求，并获取数据
            req.requestAll();
            ++total_request;
        }

        if (0){
            static VSMaintainSet reqset;
            reqset.setUINotify(&state);
            reqset.requestSetMile();
            ++total_request;
        }
        if (0){
            static VSMaintainGetRemind reqset;
            reqset.setUINotify(&state);
            reqset.requestAll();
            ++total_request;
        }
        if (0){
            static VSMaintainRecordAdd reqset;
            reqset.setUINotify(&state);
            reqset.requestAll();
            ++total_request;
        }
        if (1){
            static VSMaintainRecordPage reqset;
            reqset.setUINotify(&state);
            reqset.requestAll();
            ++total_request;
        }
    }

    if (0){
        // 车机埋点, 上传
        EventTrackIn in;
        in.eventId = "010701B";
        in.eventParam = "0";
        in.viewNum = 23;
        in.viewSuccessNum = 22;
        in.uploadTime = "2022-01-10 17:54:44";

        static VSEventTrack req;
        req.setUINotify(&state);
        // 开始请求，并上传数据
        req.postEventInfo(in);
        ++total_request;
    }

    if (1) {
#if 1
        // 上传文字信息
        if (1) {
            FeedbackIn2 in;
            in.name = "ChenMeng";
            in.phone = "88888888";
            in.feedbackMode = 2; // 文字信息
            in.feedbackType = 1; // 反馈类型共分为: 1:质量反馈 2:服务反馈 3:投诉 4:建议 5:其它
            in.feedbackContent = "反馈类型共分为";
            in.feedbackCreateDate = "2024-06-12 10:14:00";

            static VSFeedback req;
            req.setUINotify(&state);
            // 开始请求，上传数据
            req.postEventInfo2(in);
            ++total_request;
        }

        // curl -F "file=./.out-DT001/test2.wav" "http://icvservice.dflzm.com:7600/lq/vs/feedback/upload?vin=LGGG3DV20LL712313&name=test&phone=88888888&feedbackType=1&feedbackMode=1&feedbackCreateDate=2024-06-12%2010:14:00"

        // 传入音频文件
        if (0){
            FeedbackIn2 in;
            in.name = "test";
            in.phone = "88888888";
            in.feedbackMode = 1; // 语音文件
            in.feedbackType = 1; // 反馈类型共分为: 1:质量反馈 2:服务反馈 3:投诉 4:建议 5:其它
            in.file = "/tmp/adb.1000.log";
            in.feedbackCreateDate = "2024-06-12 10:14:00";
            static VSFeedback req;
            req.setUINotify(&state);
            // 开始请求，上传数据
            req.postEventInfo2(in);
            ++total_request;
        }
#else
        // 旧接口, 无效了
        // 上传文字信息
        {
            FeedbackIn in;
            in.fname = "ChenMeng";
            in.fphone = "88888888";
            in.type = 2; // 文字信息
            in.feedbackType = 1; // 反馈类型共分为: 1:质量反馈 2:服务反馈 3:投诉 4:建议 5:其它
            in.content = "{}";
            in.uploadTime = "2024-06-1210:14:00";

            static VSFeedback req;
            req.setUINotify(&state);
            // 开始请求，上传数据
            req.postEventInfo(in);
            ++total_request;
        }

        // 传入音频文件
        if (0){
            FeedbackIn in;
            in.fname = "test";
            in.fphone = "88888888";
            in.type = 1; // 语音文件
            in.feedbackType = 1; // 反馈类型共分为: 1:质量反馈 2:服务反馈 3:投诉 4:建议 5:其它
            in.file = "/tmp/clash-verge-check.yaml";
            in.uploadTime = "2024-06-1210:14:00";
            static VSFeedback req;
            req.setUINotify(&state);
            // 开始请求，上传数据
            req.postEventInfo(in);
            ++total_request;
        }
#endif
    }

    if (0){
        if (0){
            static SurveySelectQuery reqset;
            reqset.setUINotify(&state);
            reqset.requestAll();
            ++total_request;
        }
        if (0){
            static SurveySelectDetail reqset;
            reqset.setUINotify(&state);
            reqset.requestAll();
            ++total_request;
        }
        if (0) {
            ListTable<SurveySelectDetailLNode> mNodes;
            for (int i = 0; i < 3; ++i) {

                SurveySelectDetailLNode tmp;

                tmp.id = ByteString::allocString("aaaa");
                for (int j = 0; j < 0; ++j) {
                    SurveySelectDetailOptionLNode itmp;
                    itmp.optionNo = ByteString::allocString("bbbbb");
                    tmp.options.push_back(itmp);
                }
                mNodes.push_back(tmp);
            }

            {
                ListTable<SurveySelectDetailLNode>::iterator it;
                for (it = mNodes.pop_front(); it != mNodes.end(); it = mNodes.pop_front())
                {


                    it->clear();
                    mNodes.remove(it);
                }
            }
            //++total_request;
        }

        if (1) {
            static SurveySubmit reqset;
            reqset.setUINotify(&state);
            SurveySubmit::AnswersList lst;
            //SurveySubmit::Answer ans ;
            //SurveySubmit::Answer ans1 ;
            lst.push_back({
                              "bcd96fa39ba3a385ab70e8ca9ae11960",
                              {"a3bbe6bf7894cb9aa6b0a7f54a1d1eca"}
                          });
            lst.push_back({
                              "53eb06fa96451cdd69bfc956b1406c95",
                              {"7389af5489a46acee533ee65d6387a1e", "50d903b912fc3dd09dd3989ba8ec8756"}
                          });
            reqset.submit("99a264737d874e97d101688368328487",
                          "2021-12-15 16:11:21",
                          lst);
            ++total_request;

        }
    }


    if (0){
        if (0){
            static NewsTypeList reqset;
            reqset.setUINotify(&state);
            reqset.requestAll();
            ++total_request;
        }
        if (1){
            static NewsSelectPage reqset;
            reqset.setUINotify(&state);
            reqset.requestAll();
            ++total_request;
        }
    }
    if (0) {
        static ServicePolicy reqset;
        reqset.setUINotify(&state);
        reqset.requestAll();
        ++total_request;
    }

    // 图片文件下载
    if (0) {
        SFString id  = "1231231"; // 图片ID信息
        SFString url = SFString(LQ_BASE_URL "/file/imgRead?id=%1").arg(id);
        ImageCacheManage::instance()->loadImage(ByteString(url.c_str(), url.size()), &imageState);
    }

    while (state.totals() != total_request)
    {
        sleep(1);
    }

    GEN_Printf(LOG_DEBUG, "request over.");

    return 0;
}


